package com.yupi.oj.ojMQ.orderMQ;

import com.rabbitmq.client.Channel;
import com.yupi.oj.judge.JudgeService;
import com.yupi.oj.model.entity.VoucherOrder;
import com.yupi.oj.ojMQ.Ojconstant;
import com.yupi.oj.service.VoucherOrderService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;

@Component
@Slf4j
public class OrderMessageConsumer {
    @Resource
    private VoucherOrderService voucherOrderService;

//指定监听的消息队列和确认机制
    @SneakyThrows
    @RabbitListener(queues = {orderConstant.QUEUE_NAME},ackMode = "MANUAL")
    public void receiveMessage(VoucherOrder message, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long delivery_tag){
        log.info("接受消息={}",message);
        try {
            voucherOrderService.createVoucherOrder(message);
            channel.basicAck(delivery_tag,false);
        } catch (IOException e) {
            channel.basicNack(delivery_tag,false,false);
        }
    }
}
